﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using ERP_RORZE_SYSTEM;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ERP_RORZE_DLL.BasicList
{
    public partial class frm_Edit_Supplier : Form
    {
        private INISettings mySetting;
        private SqlConnection myConnection;
        private string error;

        public string supplier_id;

        public frm_Edit_Supplier()
        {
            InitializeComponent();

            this.mySetting = new INISettings();
            this.myConnection = new SqlConnection(mySetting.SQLConnectionString);
            this.error = null;
            this.supplier_id = null;
            LoadCountry();
            LoadDeliveryTerm();
            LoadPayment();
            this.countryID.Properties.AutoComplete = true;
            this.deliveryTerm.Properties.AutoComplete = true;
            this.paymentID.Properties.AutoComplete = true;            
        }

        ////////////////////////////////////
        /// Load data to form
        ///////////////////////////////////        
        public bool LoadData()
        {
            bool ret = true;

            if (string.IsNullOrEmpty(supplier_id))
            {
                ret = false;
            }
            else
            {
                try
                {
                    myConnection.Open();
                    SqlCommand cmd_sql = myConnection.CreateCommand();
                    cmd_sql.CommandText = "SELECT * FROM [222_Supplier] WHERE SupplierID='" + supplier_id + "'";
                    SqlDataReader dataReader = cmd_sql.ExecuteReader();
                    if (dataReader.Read())
                    {
                        this.supplierID.Text = dataReader["SupplierID"].ToString();
                        this.supplierName.Text = dataReader["SupplierName"].ToString();
                        this.supplierPO.Text = dataReader["SupplierID_PO"].ToString();
                        this.companyText.Text = dataReader["Company"].ToString();
                        this.addressText.Text = dataReader["Address"].ToString();
                        this.telephoneNumber.Text = dataReader["Telephone"].ToString();
                        this.faxNumber.Text = dataReader["Fax"].ToString();
                        
                        for (int i = 0; i < this.countryID.Properties.Items.Count; i++)
                        {
                            ComboboxItem item = (ComboboxItem)this.countryID.Properties.Items[i];
                            if (item.Value.ToString() == dataReader["CountryCode"].ToString())
                            {
                                this.countryID.SelectedIndex = i;
                            }
                        }

                        this.picName.Text = dataReader["PIC"].ToString();
                        this.picSex.Text = dataReader["PICSex"].ToString();
                        this.picEmail.Text = dataReader["PICEmail"].ToString();
                        this.picPosition.Text = dataReader["PICPosition"].ToString();
                        for (int i = 0; i < this.deliveryTerm.Properties.Items.Count; i++)
                        {
                            ComboboxItem item = (ComboboxItem)this.deliveryTerm.Properties.Items[i];
                            if (item.Value.ToString() == dataReader["DeliveryTermID"].ToString())
                            {
                                this.deliveryTerm.SelectedIndex = i;
                            }
                        }
                        this.comodityName.Text = dataReader["NameOfCommodity"].ToString();
                        for (int i = 0; i < this.paymentID.Properties.Items.Count; i++)
                        {
                            ComboboxItem item = (ComboboxItem)this.paymentID.Properties.Items[i];
                            if (item.Value.ToString() == dataReader["PaymentID"].ToString())
                            {
                                this.paymentID.SelectedIndex = i;
                            }
                        }
                        this.bankAccount.Text = dataReader["BankAccount"].ToString();
                        this.bankName.Text = dataReader["BankName"].ToString();
                        this.taxCode.Text = dataReader["TaxCode"].ToString();
                        this.noteText.Text = dataReader["Note"].ToString();
                    }
                    dataReader.Close();
                    cmd_sql.Dispose();
                    myConnection.Close();
                }
                catch (Exception ex)
                {
                    if (myConnection.State == ConnectionState.Open)
                    {
                        myConnection.Close();
                    }
                    ret = false;
                    MessageBox.Show(ex.Message);
                }
            }

            return ret;
        }

        ///////////////////////////////////////
        /// Load Country List
        ///////////////////////////////////////
        private void LoadCountry()
        {
            try
            {
                myConnection.Open();
                SqlCommand cmd_sql = myConnection.CreateCommand();
                cmd_sql.CommandText = "SELECT CountryCode, CountryName FROM [222_Country]";
                SqlDataReader dataReader = cmd_sql.ExecuteReader();
                while (dataReader.Read())
                {
                    ComboboxItem item = new ComboboxItem();
                    item.Value = dataReader["CountryCode"].ToString();
                    item.Text = dataReader["CountryName"].ToString();
                    this.countryID.Properties.Items.Add(item);
                }
                dataReader.Close();
                cmd_sql.Dispose();
                myConnection.Close();
            }
            catch
            {
                if (myConnection.State == ConnectionState.Open)
                {
                    myConnection.Close();
                }
                MessageBox.Show("Cannot load Country List");
            }
        }

        ///////////////////////////////////////
        /// Load Delivery List
        ///////////////////////////////////////
        private void LoadDeliveryTerm()
        {
            try
            {
                myConnection.Open();
                SqlCommand cmd_sql = myConnection.CreateCommand();
                cmd_sql.CommandText = "SELECT DeliveryTermID, DeliveryTermName FROM [222_DeliveryTerm]";
                SqlDataReader dataReader = cmd_sql.ExecuteReader();
                while (dataReader.Read())
                {
                    ComboboxItem item = new ComboboxItem();
                    item.Value = dataReader["DeliveryTermID"].ToString();
                    item.Text = dataReader["DeliveryTermName"].ToString();
                    this.deliveryTerm.Properties.Items.Add(item);
                }
                dataReader.Close();
                cmd_sql.Dispose();
                myConnection.Close();
            }
            catch
            {
                if (myConnection.State == ConnectionState.Open)
                {
                    myConnection.Close();
                }
                MessageBox.Show("Cannot load Delivery Term List");
            }
        }


        ///////////////////////////////////////
        /// Load Payment List
        ///////////////////////////////////////
        private void LoadPayment()
        {
            try
            {
                myConnection.Open();
                SqlCommand cmd_sql = myConnection.CreateCommand();
                cmd_sql.CommandText = "SELECT PaymentID, PaymentName FROM [222_Payment]";
                SqlDataReader dataReader = cmd_sql.ExecuteReader();
                while (dataReader.Read())
                {
                    ComboboxItem item = new ComboboxItem();
                    item.Value = dataReader["PaymentID"].ToString();
                    item.Text = dataReader["PaymentName"].ToString();
                    this.paymentID.Properties.Items.Add(item);
                }
                dataReader.Close();
                cmd_sql.Dispose();
                myConnection.Close();
            }
            catch
            {
                if (myConnection.State == ConnectionState.Open)
                {
                    myConnection.Close();
                }
                MessageBox.Show("Cannot load Payment List");
            }
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        private void buttonAdd_Click(object sender, EventArgs e)
        {
            error = null;

            ComboboxItem item = new ComboboxItem();

            string supplier_name = null;
            string supplier_po = null;
            string company = null;
            string address = null;
            string telephone = null;
            string fax = null;
            string country = null;
            string pic = null;
            string picsex = null;
            string picemail = null;
            string picposition = null;
            string delivery = null;
            string commodity = null;
            string payment = null;
            string bankname = null;
            string taxcode = null;
            string bankaccount = null;
            string note = null;

            if (string.IsNullOrEmpty(this.supplierName.Text))
            {
                error = "Please enter Supplier ID";
            }
            else
            {
                supplier_name = this.supplierName.Text;
            }

            if (string.IsNullOrEmpty(error))
            {
                supplier_po = this.supplierPO.Text;
                company = this.companyText.Text;
                address = this.addressText.Text;
                telephone = this.telephoneNumber.Text;
                fax = this.faxNumber.Text;
                if (this.countryID.SelectedIndex >= 0)
                {
                    item = (ComboboxItem)this.countryID.SelectedItem;
                    country = item.Value.ToString();
                }
                else
                {
                    country = "18";
                }
                pic = this.picName.Text;
                picsex = this.picSex.Text;
                picemail = this.picEmail.Text;
                picposition = this.picPosition.Text;
                if (this.deliveryTerm.SelectedIndex >= 0)
                {
                    item = (ComboboxItem)this.deliveryTerm.SelectedItem;
                    delivery = item.Value.ToString();
                }
                else
                {
                    delivery = "NF";
                }
                commodity = this.comodityName.Text;
                if (this.paymentID.SelectedIndex >= 0)
                {
                    item = (ComboboxItem)this.paymentID.SelectedItem;
                    payment = item.Value.ToString();
                }
                else
                {
                    payment = "NoComercia";
                }
                taxcode = this.taxCode.Text;
                bankaccount = this.bankAccount.Text;
                bankname = this.bankName.Text;
                note = this.noteText.Text;                
                            
                try
                {
                    myConnection.Open();
                    string sql = "UPDATE [222_Supplier] SET [SupplierID_PO]='" + supplier_po + "',"
                        + " [SupplierName]=N'" + supplier_name + "', [Company]=N'" + company + "', Address=N'" + address + "',"
                        + " [CountryCode] = '" + country + "', [Telephone]='" + telephone + "', [Fax] = '" + fax + "',"
                        + " [PICSex] = '" + picsex + "', [PIC] = '" + pic + "', [PICEmail] = '" + picemail + "',"
                        + " [PICPosition] = N'" + picposition + "', [NameOfCommodity] = N'" + commodity + "',"
                        + " [DeliveryTermID] = '" + delivery + "', [PaymentID] = '" + payment + "', [Note] = '" + note + "',"
                        + " [BankAccount] = '" + bankaccount + "', [BankName] = N'" + bankname + "', [TaxCode] = '" + taxcode + "'"
                        + " WHERE SupplierID='" + supplier_id + "'";                    
                    SqlCommand cmd_sql = new SqlCommand(sql, myConnection);                    
                    cmd_sql.ExecuteNonQuery();
                    cmd_sql.Dispose();
                    myConnection.Close();
                    MessageBox.Show("Supplier updated success.");
                    this.Dispose();
                }
                catch (Exception ex)
                {
                    if (myConnection.State == ConnectionState.Open)
                    {
                        myConnection.Close();
                    }
                    MessageBox.Show(ex.Message);
                    //MessageBox.Show("Cannot update Supplier. Please try again.");
                }
            }
            else
            {
                MessageBox.Show(error);
            }
        }
    }
}
